import Vue from 'vue'
import Vuex from 'vuex'

Vue.use(Vuex)

// 模拟餐品数据
const foodData = {
  '啤酒1': [
    { id: 1, name: '德式小麦1', image: '/static/logo.png', desc: '111', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
    { id: 2, name: '德式小麦2', image: '/static/logo.png', desc: '111', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 24 },
					{ size: '3L', price: 28 }
				], count: 0 },
    { id: 3, name: '德式小麦3', image: '/static/logo.png', desc: '222', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 48 }
				], count: 0 },
    { id: 4, name: '德式小麦4', image: '/static/logo.png', desc: '333', price: [
					{ size: '1L', price: 16 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
	{ id: 11, name: '德式小麦1', image: '/static/logo.png', desc: '111', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 98 }
				], count: 0 },
	{ id: 12, name: '德式小麦2', image: '/static/logo.png', desc: '111', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 28 },
					{ size: '3L', price: 28 }
				], count: 0 },
	{ id: 13, name: '德式小麦3', image: '/static/logo.png', desc: '222', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
	{ id: 14, name: '德式小麦4', image: '/static/logo.png', desc: '333', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
	{ id: 21, name: '德式小麦1', image: '/static/logo.png', desc: '111', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
	{ id: 22, name: '德式小麦2', image: '/static/logo.png', desc: '111', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
	{ id: 23, name: '德式小麦3', image: '/static/logo.png', desc: '222', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
	{ id: 24, name: '德式小麦4', image: '/static/logo.png', desc: '333', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 }
  ],
  '啤酒2': [
    { id: 1, name: '德式小麦1', image: '/static/logo.png', desc: '666', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 2, name: '德式小麦2', image: '/static/logo.png', desc: '444', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 3, name: '德式小麦3', image: '/static/logo.png', desc: '555', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 4, name: '德式小麦4', image: '/static/logo.png', desc: '666', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 }
  ],
  '啤酒3': [
    { id: 1, name: '德式小麦1', image: '/static/logo.png', desc: '222', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 2, name: '德式小麦2', image: '/static/logo.png', desc: '777', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 3, name: '德式小麦3', image: '/static/logo.png', desc: '888', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 4, name: '德式小麦4', image: '/static/logo.png', desc: '111', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 }
  ],
  '啤酒4': [
    { id: 1, name: '德式小麦1', image: '/static/logo.png', desc: '222', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 2, name: '德式小麦2', image: '/static/logo.png', desc: '333', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 3, name: '德式小麦3', image: '/static/logo.png', desc: '555', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 },
        { id: 4, name: '德式小麦4', image: '/static/logo.png', desc: '999', price: [
					{ size: '1L', price: 12 },
					{ size: '2L', price: 20 },
					{ size: '3L', price: 28 }
				], count: 0 }
  ]
}

export default new Vuex.Store({
  state: {
    foodCategories: Object.keys(foodData), // 食品分类列表
    foodData: foodData, // 食品数据
    cart: [{
    "id": "2_1L",
    "name": "德式小麦2 (1L)",
    "image": "/static/logo.png",
    "desc": "111",
    "price": 12,
    "count": 1,
    "spec": "1L",
    "category": "啤酒1",
	"checked": true
}], // 购物车
    currentCategory: '啤酒1', // 当前选中的分类
    deliveryFee: 5, // 配送费
    deliveryFreeThreshold: 50, // 免配送费阈值
    orderMinimum: 20, // 最低下单金额
    takeoutInfo: {
      address: '',
      phone: '',
	  name: '',
      needTableware: true,
      remark: ''
    }
  },
  getters: {
    // 获取当前分类的食品列表
    currentCategoryFoods(state) {
      return state.foodData[state.currentCategory] || []
    },
    // 计算购物车总价
    cartTotal(state, getters) {
      return getters.checkedList.reduce((total, item) => total + item.price * item.count, 0)
    },
    // 计算购物车中的商品总数
    cartCount(state) {
      return getters.checkedList.reduce((count, item) => count + item.count, 0)
    },
    // 判断是否达到最低下单金额
    canCheckout(state, getters) {
      return getters.cartTotal >= state.orderMinimum
    },
    // 计算是否需要配送费
    needDeliveryFee(state, getters) {
      return getters.cartTotal < state.deliveryFreeThreshold
    },
    // 计算最终支付金额（含配送费）
    finalTotal(state, getters) {
      return getters.cartTotal + (getters.needDeliveryFee ? state.deliveryFee : 0)
    },
	// checked为true的商品列表
	checkedList(state, getters) {
		return state.cart.filter(item => item.checked === true)
	},
	//购物车'全选'状态
	selectAll(state, getters) {
		return state.cart.length === getters.checkedList.length
	}
  },
  mutations: {
    // 设置当前分类
    setCurrentCategory(state, category) {
      state.currentCategory = category
    },
    
    // 清空购物车
    clearCart(state) {
      // 重置所有食品数量
      Object.keys(state.foodData).forEach(category => {
        state.foodData[category].forEach(food => {
          food.count = 0
        })
      })
      
      // 清空购物车
      state.cart = []
    },
    // 更新外带信息
    updateTakeoutInfo(c, info) {
      state.takeoutInfo = {...state.takeoutInfo, ...info}
    },
	checkedSolo(state, index) {
		state.cart[index].checked = !state.cart[index].checked
	},
	// 购物车页面'全选'按钮事件
	checkedAll(state) {
		if(this.getters.selectAll) {
			state.cart.forEach(item => item.checked = false)
		}
		else {
			state.cart.forEach(item => item.checked = true)
		}
	},
	// 购物车删除选中的商品
	delChecked(state) {
		state.cart = state.cart.filter(item => item.checked === false)
	}
  },
  actions: {
    // 提交订单
    submitOrder({state, getters, commit}, orderType) {
      return new Promise((resolve) => {
        // 模拟提交订单
        const orderInfo = {
          orderType, // 堂食或外带
          items: [...state.cart],
          totalPrice: getters.cartTotal,
          finalPrice: getters.finalTotal,
          deliveryFee: orderType === 'takeout' && getters.needDeliveryFee ? state.deliveryFee : 0,
          takeoutInfo: orderType === 'takeout' ? state.takeoutInfo : null,
          orderTime: new Date().toLocaleString()
        }
        
        // 模拟API请求延迟
        setTimeout(() => {
          console.log('订单已提交:', orderInfo)
          // 清空购物车
          commit('clearCart')
          resolve(orderInfo)
        }, 500)
      })
    }
  }
})